package hcqs.nav.grid.map.tools;

import hcqs.nav.grid.consts.GridMapConsts;
import hcqs.nav.grid.macros.Cell;
import hcqs.nav.grid.macros.Point;
import hcqs.nav.grid.map.macros.GridMapRectArea;

import java.io.*;

public class FileHelper {
    public void save(String mapId, int cellSize, Point op, byte[][] data) {
        saveDescFile(mapId, cellSize, op, data);
        saveDataFile(mapId, cellSize, data);
    }

    private void saveDataFile(String mapId, int cellSize, byte[][] data) {
        String fn = String.format("%s/%s/%sData", GridMapConsts.MAP_SAVE_PATH_BASE, mapId, cellSize);
        File f = new File(fn);
        if (!f.getParentFile().exists())
            f.getParentFile().mkdir();
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        OutputStream out = null;
        try {
            out = new FileOutputStream(fn);
            for (int row = 0; row < data.length; row++) {
                out.write(data[row]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
    }

    private void saveDescFile(String mapId, int cellSize, Point op, byte[][] data) {
        String fn = String.format("%s/%s/%sDesc", GridMapConsts.MAP_SAVE_PATH_BASE, mapId, cellSize);
        File f = new File(fn);
        if (!f.getParentFile().exists())
            f.getParentFile().mkdir();
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedWriter bufWriter = null;
        try {
            bufWriter = new BufferedWriter(new FileWriter(fn));
            bufWriter.write(String.format("mapId=%s\n", mapId));
            bufWriter.write(String.format("cellSize=%s\n", cellSize));
            bufWriter.write(String.format("op=%s,%s\n", op.getX(), op.getY()));
            bufWriter.write(String.format("size:height=%s,width=%s\n", data.length, data[0].length));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufWriter != null) {
                try {
                    bufWriter.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
    }

    public GridMapRectArea load(String mapId, String cellSize) {
        GridMapRectArea map = new GridMapRectArea();
        Point op = readDescFile(mapId, cellSize);
        Cell mapLW = readDescFileMapHW(mapId, cellSize);
        if (mapLW == null)
            return null;
        map.setOp(op);
        map.setArea(readDataFile(mapId, cellSize, mapLW.getY(), mapLW.getX()));
        return map;
    }

    private byte[][] readDataFile(String mapId, String cellSize, int height, int width) {
        File f = new File(String.format("%s/%s/%sDesc", GridMapConsts.MAP_SAVE_PATH_BASE, mapId, cellSize));
        InputStream in = null;
        try {
            in = new FileInputStream(f);
            byte[][] data = new byte[height][width];
            for (int row = 0; row < height; row++) {
                in.read(data[row]);
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
    }

    private Cell readDescFileMapHW(String mapId, String cellSize) {
        File f = new File(String.format("%s/%s/%sDesc", GridMapConsts.MAP_SAVE_PATH_BASE, mapId, cellSize));
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            reader.readLine();
            reader.readLine();
            reader.readLine();
            String content = reader.readLine();
            String value = content.split(":")[1];
            Cell c = new Cell();
            c.setY(Integer.parseInt(value.split(",")[0].split("=")[1]));
            c.setX(Integer.parseInt(value.split(",")[1].split("=")[1]));
            return c;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
    }

    private Point readDescFile(String mapId, String cellSize) {
        File f = new File(String.format("%s/%s/%sDesc", GridMapConsts.MAP_SAVE_PATH_BASE, mapId, cellSize));
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            reader.readLine();
            reader.readLine();
            String content = reader.readLine();
            String value = content.split("=")[1];
            Point op = new Point();
            op.setX(Integer.parseInt(value.split(",")[0]));
            op.setY(Integer.parseInt(value.split(",")[1]));
            return op;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.getStackTrace();
                }
            }
        }
    }
}
